using Godot;

namespace MyCraft2;

public partial class SelectedBlockRect : TextureRect
{
	public static Block SelectedBlock { get;private set; }
	private static int _selectedBlockIndex;
	private ImageTexture _imageTexture;
	public override void _Ready()
	{
		_imageTexture = new ImageTexture();
		Texture = _imageTexture;

		SelectedBlock = Block.Instances[_selectedBlockIndex];
		_imageTexture.SetImage(Block.TextureArray._Images[Block.Instances[_selectedBlockIndex].ForwardTexture.Id]);
	}

	public override void _Input(InputEvent @event)
	{
		base._Input(@event);

		if (@event is InputEventMouseButton mouseButton)
		{
			if (mouseButton.ButtonIndex is MouseButton.WheelUp) _selectedBlockIndex += 1;
			else if (mouseButton.ButtonIndex is MouseButton.WheelDown) _selectedBlockIndex -= 1;
			if(_selectedBlockIndex == Block.Instances.Count)_selectedBlockIndex = 0;
			if (_selectedBlockIndex < 0) _selectedBlockIndex = Block.Instances.Count - 1;
			SelectedBlock = Block.Instances[_selectedBlockIndex];
			_imageTexture.SetImage(Block.TextureArray._Images[Block.Instances[_selectedBlockIndex].ForwardTexture.Id]);
		}
	}
}
